package com.test.view2img;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class View2Img extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.v2i);

		//
		// findViewById(R.id.txt).setVisibility(View.INVISIBLE);
		findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});
		final View content = findViewById(R.id.content);
		content.getViewTreeObserver().addOnGlobalLayoutListener(
				new ViewTreeObserver.OnGlobalLayoutListener() {
					public void onGlobalLayout() {
						int width = content.getWidth();
						int height = content.getHeight();
						LinearLayout viewToBeConverted = (LinearLayout) findViewById(R.id.content);
						Bitmap viewBitmap = Bitmap.createBitmap(width, height,
								Bitmap.Config.RGB_565);
						Canvas canvas = new Canvas(viewBitmap);
						Paint paint = new Paint();
						paint.setColor(Color.WHITE);
						paint.setStyle(Style.FILL);
						canvas.drawText("test", 100, 300, paint);
						viewToBeConverted.draw(canvas);
						ImageView imgV = (ImageView) findViewById(R.id.img);
						imgV.setImageBitmap(viewBitmap);
					}
				});

	}
}
